package com.jeesite.modules.sso.web;

import com.jeesite.common.shiro.authc.FormToken;
import com.jeesite.common.shiro.filter.FormFilter;
import com.jeesite.common.web.http.ServletUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @projectName: whgadc
 * @package: com.jeesite.modules.sso.web
 * @className: SSOController
 * @description: oa系统集成的pki登录成功后回调地址，实现本地自动登录
 * @date: 2023/11/28 14:05
 * @version: 1.0
 */
@Controller
@RequestMapping(value = "${adminPath}/pki/sso")
public class PkiSSOController {

    @RequestMapping(value = "index")
    public String index(Model model, String jh, String userName) {
        HttpServletRequest request = ServletUtils.getRequest();
        HttpServletResponse response = ServletUtils.getResponse();
        try {
            // FormToken 构造方法的三个参数：登录名、是否内部登录无条件、请求对象
            UserUtils.getSubject().login(new FormToken(jh, true, request));
            System.out.println("登录成功，__sid=" + UserUtils.getSession().getId());
            FormFilter.onLoginSuccess(request, response);
        } catch (AuthenticationException e) {
            FormFilter.onLoginFailure(e, request, response);
        }
        return "modules/sys/sysIndex";
    }
}
